﻿using GKQC.Common;
using GKQC.DbHelper.DbModels;
using GKQC.DbHelper.ViewModels;
using GKQC.Language;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace GKQC.Ort
{



    public partial class FormOrtFilter : Form
    {
        public OrtFilter Filter;
        UserModel _User;
        public List<ReportSetModel> _ReportSetModels;

        public FormOrtFilter(UserModel user,string billType, List<ReportSetModel> reportSetModels)
        {
            InitializeComponent();

            try
            {
                this._User = user;
                this._ReportSetModels = reportSetModels.ToJson().ToObject<List<ReportSetModel>>();
                this._ReportSetModels.Insert(0, new ReportSetModel());
                if (Filter == null)
                {
                    Filter = new OrtFilter();
                    Filter.StartModiDate = this._User.GetServerTime();
                    Filter.EndModiDate = Filter.StartModiDate;
                    Filter.StartTestDate = Filter.StartModiDate;
                    Filter.EndTestDate = Filter.StartModiDate;
                    Filter.StartFinishDate = Filter.StartModiDate;
                    Filter.EndFinishDate = Filter.StartModiDate;
                    Filter.StartExpireDate = Filter.StartModiDate;
                    Filter.EndExpireDate = Filter.StartModiDate;
                    Filter.BillType = billType;
                    Filter.RowCount = 100;
                }

                txtCap.GotFocus += new EventHandler(FormCommon.TextBox_GotFocus);
                txtVoltage.GotFocus += new EventHandler(FormCommon.TextBox_GotFocus);
                txtTestTemp.GotFocus += new EventHandler(FormCommon.TextBox_GotFocus);
                GetReportSet();

                BindingControls();
            }
            catch(Exception ex)
            {
                Logger.LogError(ex);
                QMessageBox.Show(ex.Message , "異常S0028", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void BindingControls()
        {
            chkIsModiDate.DataBindings.Clear();
            chkIsTestDate.DataBindings.Clear();
            chkIsFinishDate.DataBindings.Clear();
            chkIsExpireDate.DataBindings.Clear();
            dteStartModiDate.DataBindings.Clear();
            dteEndModiDate.DataBindings.Clear();
            dteStartTestDate.DataBindings.Clear();
            dteEndTestDate.DataBindings.Clear();
            dteStartFinishDate.DataBindings.Clear();
            dteEndFinishDate.DataBindings.Clear();
            dteStartExpireDate.DataBindings.Clear();
            dteEndExpireDate.DataBindings.Clear();
            txtBillNo.DataBindings.Clear();
            txtCustomer.DataBindings.Clear();
            txtLotNo.DataBindings.Clear();
            txtSeries.DataBindings.Clear();
            txtSize.DataBindings.Clear();
            txtCap.DataBindings.Clear();
            txtVoltage.DataBindings.Clear();
            chkIsFinished.DataBindings.Clear();
            chkIsUnFinished.DataBindings.Clear();
            txtPaste.DataBindings.Clear();
            txtTestTemp.DataBindings.Clear();
            txtBoxNo.DataBindings.Clear();
            chkIsA.DataBindings.Clear();
            chkIsB.DataBindings.Clear();
            chkIsC.DataBindings.Clear();
            numRowCount.DataBindings.Clear();
            chkIsBillNo.DataBindings.Clear();
            txtStartBillNo.DataBindings.Clear();
            txtEndBillNo.DataBindings.Clear();
            chkAllBill.DataBindings.Clear();
            cboReport.DataBindings.Clear();
            cboIsValid.DataBindings.Clear();
            txtPn.DataBindings.Clear();

            if (Filter == null) return;
            chkIsModiDate.DataBindings.Add("Checked",Filter, "IsModiDate");
            chkIsTestDate.DataBindings.Add("Checked", Filter, "IsTestDate");
            chkIsFinishDate.DataBindings.Add("Checked", Filter, "IsFinishDate");
            chkIsExpireDate.DataBindings.Add("Checked", Filter, "IsExpireDate");
            dteStartModiDate.DataBindings.Add("Value", Filter, "StartModiDate"); 
            dteEndModiDate.DataBindings.Add("Value", Filter, "EndModiDate");
            dteStartTestDate.DataBindings.Add("Value", Filter, "StartTestDate");
            dteEndTestDate.DataBindings.Add("Value", Filter, "EndTestDate");
            dteStartFinishDate.DataBindings.Add("Value", Filter, "StartFinishDate");
            dteEndFinishDate.DataBindings.Add("Value", Filter, "EndFinishDate");
            dteStartExpireDate.DataBindings.Add("Value", Filter, "StartExpireDate");
            dteEndExpireDate.DataBindings.Add("Value", Filter, "EndExpireDate");
            txtBillNo.DataBindings.Add("Text", Filter, "BillNo");
            txtCustomer.DataBindings.Add("Text", Filter, "Customer");
            txtLotNo.DataBindings.Add("Text", Filter, "LotNo");
            txtSeries.DataBindings.Add("Text", Filter, "Series");
            txtSize.DataBindings.Add("Text", Filter, "Size");
            txtCap.DataBindings.Add("Text", Filter, "Cap");
            txtVoltage.DataBindings.Add("Text", Filter, "Voltage");
            chkIsFinished.DataBindings.Add("Checked", Filter, "IsFinished");
            chkIsUnFinished.DataBindings.Add("Checked", Filter, "IsUnFinished");
            txtPaste.DataBindings.Add("Text", Filter, "Paste");
            txtTestTemp.DataBindings.Add("Text", Filter, "TestTemp");
            txtBoxNo.DataBindings.Add("Text", Filter, "BoxNo");
            chkIsA.DataBindings.Add("Checked", Filter, "IsA");
            chkIsB.DataBindings.Add("Checked", Filter, "IsB");
            chkIsC.DataBindings.Add("Checked", Filter, "IsC");
            numRowCount.DataBindings.Add("Value", Filter, "RowCount");
            chkIsBillNo.DataBindings.Add("Checked", Filter, "IsBillNo");
            txtStartBillNo.DataBindings.Add("Text", Filter, "StartBillNo");
            txtEndBillNo.DataBindings.Add("Text", Filter, "EndBillNo");
            chkAllBill.DataBindings.Add("Checked", Filter, "AllBill");
            cboReport.DataBindings.Add("SelectedValue", Filter, "ReportId");
            cboIsValid.DataBindings.Add("SelectedIndex", Filter, "IsValidIndex");
            txtPn.DataBindings.Add("Text", Filter, "Pn");
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            this.Validate();
        }

        private void txtCap_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (Filter == null) return;
                FormCommon.TextBoxNoDisplayZero_TextChanged(Filter,sender, e);
            }
            catch(Exception ex)
            {
                Logger.LogError(ex);
            }
        }

        private void FormOrtFilter_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyCode == Keys.Enter)
                {
                    this.SelectNextControl(this.ActiveControl, true, true, true, true);
                }
            }
            catch(Exception ex)
            {
                Logger.LogError(ex);
            }
        }

        private void GetReportSet()
        {
            try
            {
                if (_ReportSetModels != null)
                {
                    cboReport.DisplayMember = "Name";
                    cboReport.ValueMember = "Id";
                    cboReport.DataSource = _ReportSetModels;
                }
            }
            catch (Exception ex)
            {
                QMessageBox.Show(ex.Message, "異常S0020", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void cboReport_TextChanged(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(cboReport.Text)) Filter.ReportId = 0;
        }
    }
}
